<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Auth {

    public function __construct() {
        $this->CI = & get_instance();
    }

    public function cek_auth($username = '', $password = '') {
        $sql = "SELECT * FROM t_member WHERE member_nama=? AND member_pass=? AND member_aktifasi='1'";
        $query = $this->CI->db->query($sql, array($username, md5($password)));

        if ($query->num_rows() == 1) {
            foreach ($query->result() as $row) {
                $data_user = array(
                    'member_id' => $row->member_id,
                    'member_nama' => $row->member_nama,
                    'member_namanya' => $row->member_namanya,
                    'member_kelamin' => $row->member_kelamin,
                    'member_role' => $row->member_role,
                    'member_tanggal' => $row->member_tanggal,
                    'member_masuk' => TRUE
                );
            }
            // set untuk session
            $this->CI->session->set_userdata($data_user);

            //jika ada set TRUE
            return TRUE;
        } else {
            return FALSE;
        }
    }

    // yang sudah masuk
    public function log_in() {
        if ($this->CI->session->userdata('member_masuk')) {
            return TRUE;
        }
    }

    // pengen keluar
    public function log_out() {
        $this->CI->session->sess_destroy();
        return TRUE;
    }

    // fungsi untuk mengatur restriction sebuah halaman
    public function restrict($log_out = FALSE) {
        if ($log_out && $this->log_in()) {
            // jika user sudah login tetapi ingin melihat halaman
            // yang tidak seharusnya di lihat
            redirect('homes/');
        }

        if (!$log_out && !$this->log_in()) {
            // jika user sudah logout (default: FALSE) dan dia ingin masuk halaman adamin
            redirect('logins/');
        }
    }
    
    public function get_member_by_idnya($id) {
        $this->CI->db->select('member_namanya');
        $this->CI->db->where('member_id', $id);
        $query = $this->CI->db->get('t_member');

        if ($query->num_rows() > 0) {
            return $query->row()->member_namanya;
        } else {
            return '';
        }
    }

    // load role
    public function load_role_member() {
        return array(
            '0' => 'Pilih',
            '1' => 'Administrator',
            '2' => 'Admin',
            '3' => 'Musaid',
            '4' => 'Santri'
        );
    }

    // ngecek role secara langsung
    public function cek_role_member() {
        $role = $this->CI->session->userdata('member_role');
        return $role;
    }

    // ngecek role (hak akses)
    public function cek_role() {
        if ($this->CI->session->userdata('member_role')) {
            $role = $this->_role_name($this->CI->session->userdata('member_role'));
            return $role;
        }
    }

    // isi role nya
    public function _role_name($role = '0') {
        switch ($role) {
            case '1' : $roles = 'Administrator';
                break;
            case '2' : $roles = 'Admin';
                break;
            case '3' : $roles = 'Musaid';
                break;
            case '4' : $roles = 'Santri';
        }

        return $roles;
    }

    public function _menu_member($role) {
        switch ($role) {
            case 'Administrator' : $this->CI->load->view('cms/mnu_admin');
                break;
            case 'Dosen' : $this->CI->load->view('cms/mnu_dosen');
                break;
            case 'Karyawan' : $this->CI->load->view('cms/mnu_karyawan');
                break;
            case 'Mahasiswa' : $this->CI->load->view('cms/mnu_mahasiswa');
                break;
            case 'Guest' : $this->CI->load->view('cms/mnu_guest');
                break;
        }
    }

    public function load_kelamin() {
        return array(
            '0' => 'Pilih',
            '1' => 'Laki-Laki',
            '2' => 'Perempuan'
        );
    }

    public function _kelamin_name($role = '0') {
        switch ($role) {
            case '1' : $roles = 'Laki-Laki';
                break;
            case '2' : $roles = 'Perempuan';
                break;
        }

        return $roles;
    }

    public function load_status_santri() {
        return array(
            '0' => 'Pilih',
            '1' => 'Aktif',
            '2' => 'Lulus',
            '3' => 'Keluar'
        );
    }

    public function _status_santri($role = '0') {
        switch ($role) {
            case '1' : $roles = 'Aktif';
                break;
            case '2' : $roles = 'Lulus';
                break;
            case '3' : $roles = 'Keluar';
        }

        return $roles;
    }

    public function load_sekolah_santri() {
        return array(
            '0' => 'Pilih',
            '1' => 'MTS',
            '2' => 'MA',
            '3' => 'Pesantren'
        );
    }

    public function _sekolah_santri($role = '0') {
        switch ($role) {
            case '1' : $roles = 'MTS';
                break;
            case '2' : $roles = 'MA';
                break;
            case '3' : $roles = 'Pesantren';
        }

        return $roles;
    }
}

/* End of file Auth.php */
/* Location: ./application/library/Auth.php */